package com.jason.library.immersion.utils

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.content.res.Resources
import android.content.res.Resources.NotFoundException
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import com.jason.library.immersion.model.Constants

/**
 * Description:
 *
 * @author chenbaoyang
 * @create 2021/1/11 17:28
 */
object BarUtils {
    fun getStatusBarHeight(context: Context): Int {
        return getInternalDimensionSize(context, Constants.IMMERSION_STATUS_BAR_HEIGHT)
    }

    private fun getInternalDimensionSize(
        context: Context,
        key: String
    ): Int {
        val result = 0
        try {
            val resourceId =
                Resources.getSystem().getIdentifier(key, "dimen", "android")
            if (resourceId > 0) {
                val sizeOne = context.resources.getDimensionPixelSize(resourceId)
                val sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId)
                return if (sizeTwo >= sizeOne) {
                    sizeTwo
                } else {
                    val densityOne =
                        context.resources.displayMetrics.density
                    val densityTwo =
                        Resources.getSystem().displayMetrics.density
                    val f = sizeOne * densityTwo / densityOne
                    (if (f >= 0) f + 0.5f else f - 0.5f).toInt()
                }
            }
        } catch (ignored: NotFoundException) {
            return 0
        }
        return result
    }

    @SuppressLint("NewApi")
    fun getSmallestWidthDp(activity: Activity): Float {
        val metrics = DisplayMetrics()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            activity.windowManager.defaultDisplay.getRealMetrics(metrics)
        } else {
            activity.windowManager.defaultDisplay.getMetrics(metrics)
        }
        val widthDp = metrics.widthPixels / metrics.density
        val heightDp = metrics.heightPixels / metrics.density
        return Math.min(widthDp, heightDp)
    }

    fun isNavigationAtBottom(activity: Activity): Boolean {
        return getSmallestWidthDp(activity) >= 600 ||
                activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    @TargetApi(14)
    fun getNavigationBarHeight(context: Context): Int {
        val result = 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            if (hasNavBar(context as Activity)) {
                val key: String
                if (context.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT ) {
                    key = Constants.IMMERSION_NAVIGATION_BAR_HEIGHT
                } else {
                    key = Constants.IMMERSION_NAVIGATION_BAR_HEIGHT_LANDSCAPE
                }
                return getInternalDimensionSize(context, key)
            }
        }
        return result
    }

    @TargetApi(14)
    fun getNavigationBarWidth(context: Context): Int {
        val result = 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            if (hasNavBar(context as Activity)) {
                return getInternalDimensionSize(context, Constants.IMMERSION_NAVIGATION_BAR_WIDTH)
            }
        }
        return result
    }

    @TargetApi(14)
    private fun hasNavBar(activity: Activity): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //判断小米手机是否开启了全面屏，开启了，直接返回false
            if (Settings.Global.getInt(
                    activity.contentResolver,
                    Constants.IMMERSION_MIUI_NAVIGATION_BAR_HIDE_SHOW,
                    0
                ) != 0
            ) {
                return false
            }
            //判断华为手机是否隐藏了导航栏，隐藏了，直接返回false
            if (OSUtils.isEMUI()) {
                if (OSUtils.isEMUI3_x() || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    if (Settings.System.getInt(
                            activity.contentResolver,
                            Constants.IMMERSION_EMUI_NAVIGATION_BAR_HIDE_SHOW,
                            0
                        ) != 0
                    ) {
                        return false
                    }
                } else {
                    if (Settings.Global.getInt(
                            activity.contentResolver,
                            Constants.IMMERSION_EMUI_NAVIGATION_BAR_HIDE_SHOW,
                            0
                        ) != 0
                    ) {
                        return false
                    }
                }
            }
        }
        //其他手机根据屏幕真实高度与显示高度是否相同来判断
        val windowManager = activity.windowManager
        val d = windowManager.defaultDisplay
        val realDisplayMetrics = DisplayMetrics()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            d.getRealMetrics(realDisplayMetrics)
        }
        val realHeight = realDisplayMetrics.heightPixels
        val realWidth = realDisplayMetrics.widthPixels
        val displayMetrics = DisplayMetrics()
        d.getMetrics(displayMetrics)
        val displayHeight = displayMetrics.heightPixels
        val displayWidth = displayMetrics.widthPixels
        return realWidth - displayWidth > 0 || realHeight - displayHeight > 0
    }
}